#Locking down violence 10.18.22
#Replication file 
#Figures 5 and 6
####################################################################################
#Packages
library(ggplot2)
library(tidyverse)
library(sf)

####################################################################################
rm(list=ls()) #Clearing workspace
####################################################################################

load(file= "ne_iraq_gov.shp", verbose = FALSE)

#Figure 4

load(file= "no.curf.map.Rdata", verbose = FALSE)

#Before curfew ban

ggplot(no.curf.map, aes(x=longitude , y=latitude)) +
  geom_point(size=1, color="red")+
  geom_sf(data = ne_iraq_gov, fill=NA, show.legend=FALSE, lwd=.5) + #layers province outline
  stat_density_2d(alpha=2, contour = TRUE, color="orange") +
  annotate(geom="text", x=44.4, y=33.1, label="Baghdad",
           color="black", angle=15, size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=43, y=36.6, label="Mosul",
           color="black", angle=25,size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=44.37, y=35.46, label="Kirkuk",
           color="black", angle=15,size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=44.60, y=33.75, label="Baqubah",
           color="black", angle=15, size=5, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  coord_sf(xlim = c(39, 47), ylim = c(30.5, 37), expand = FALSE)+ #zooms in on the map
  theme(legend.position='none')+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_blank()) #eliminates various axis ticks and legends

#During curfew for all

load(file= "curf.map.Rdata", verbose = FALSE)

ggplot(curf.map, aes(x=longitude , y=latitude)) +
  geom_point(size=1, color="red")+
  geom_sf(data = ne_iraq_gov, fill=NA, show.legend=FALSE, lwd=.5) + #layers province outline
  stat_density_2d(alpha=2, contour = TRUE, color="orange") +
  annotate(geom="text", x=44.4, y=33.1, label="Baghdad",
           color="black", angle=15, size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=43, y=36.6, label="Mosul",
           color="black", angle=25,size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=44.37, y=35.46, label="Kirkuk",
           color="black", angle=15,size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=44.60, y=33.75, label="Baqubah",
           color="black", angle=15, size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  coord_sf(xlim = c(39, 47), ylim = c(30.5, 37), expand = FALSE)+ #zooms in on the map
  theme(legend.position='none')+
  theme_bw()+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_blank()) #eliminates various axis ticks and legends



####################################################################################
#Figure 6

#Curfew Chisquare test
load(file= "curf.chi.Rdata", verbose = FALSE)
chisq.test(curf.chi)

#Curfew Chisquare plot 
load(file= "curf.chi.plot.Rdata", verbose = FALSE)

ggplot(data=curf.chi.plot, aes(x=Location, y=Events, fill=time)) +
  geom_bar(stat="identity", position=position_dodge())+
  theme_bw()+
  scale_fill_manual(values=c("orange", "red"))+
  theme(panel.grid.major = element_blank(),
        panel.grid.minor = element_blank(),
        axis.ticks.y=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.x = element_text(family="Times New Roman", size = 11),
        axis.text.y = element_text(family="Times New Roman", size = 11),
        axis.title.x = element_text(family="Times New Roman", size = 12),
        axis.title.y = element_text(family="Times New Roman", size = 12))+
  annotate(geom="text", x=1, y=117, label="Chi-square",
           color="black", angle=0, size=4, fontface="bold",family="Times New Roman")+ #adds text on xy coordinates
  annotate(geom="text", x=1, y=110, label="p = 0.01246",
           color="black", angle=0, size=4, fontface="bold",family="Times New Roman") #adds text on xy coordinates


